package com.xiaoshuidi.cloud.module.iot;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Feng
 * @date 2023/6/19
 */
public class Test {
    public static void main(String[] args) {
        List<Info> list = new ArrayList<>();

        Info info4 = new Info(BigDecimal.valueOf(0), BigDecimal.valueOf(1.1));
        Info info1 = new Info(BigDecimal.valueOf(12), BigDecimal.valueOf(2.2));
        Info info2 = new Info(BigDecimal.valueOf(23), BigDecimal.valueOf(3.3));
        Info info3 = new Info(BigDecimal.valueOf(34), BigDecimal.valueOf(4.2));

        list.add(info3);
        list.add(info2);
        list.add(info1);
        list.add(info4);
        compute(BigDecimal.valueOf(36), BigDecimal.valueOf(40), list);
    }

    public static BigDecimal compute(BigDecimal sourceTotal, BigDecimal currentTotal, List<Info> list) {
        BigDecimal diff = currentTotal.subtract(sourceTotal);
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (int i = 0; i < list.size(); i++) {
            Info s = list.get(i);
            if (currentTotal.compareTo(s.getRead()) > 0) {
                BigDecimal currentDiff = currentTotal.subtract(s.getRead());
                if (currentDiff.compareTo(diff) < 0) {
                    BigDecimal amount = currentDiff.multiply(s.getPrice());
                    totalAmount = totalAmount.add(amount);
                    amount = compute(sourceTotal, s.getRead(), list);
                    totalAmount = totalAmount.add(amount);
                    break;
                } else {
                    BigDecimal amount = diff.multiply(s.getPrice());
                    totalAmount = totalAmount.add(amount);
                    break;
                }
            }
        }
        System.out.println(totalAmount);
        return totalAmount;
    }


    @Data
    @AllArgsConstructor
    public static class Info {
        private BigDecimal read;
        private BigDecimal price;
    }
}
